package Count;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.FileInputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 *
 * @author phamthanh
 */
public class Count {

    public static void main(String argv[]) throws XMLStreamException, IOException {
        Count c = new Count();
//        c.countNum("trainset\\hand\\num_hand.xml");
//        c.countCroup("textNor\\trainset_punc_nsw_url_split_group.xml");
        c.countPunc("textNor\\trainset_punc_nsw_url_split_group.xml");
    }

    public void countNum(String inF) throws XMLStreamException, IOException {
        long startTime = System.currentTimeMillis();
        int ntim = 0;
        int ndat = 0;
        int nday = 0;
        int nmon = 0;
        int nnum = 0;
        int ntel = 0;
        int ndig = 0;
        int nscr = 0;
        int nrgn = 0;
        int nfrc = 0;
        int nadd = 0;
        int ncom = 0;

        FileInputStream fis = new FileInputStream(inF);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("W")) {
                        String atval = xmlStreamReader.getAttributeValue(0);
                        if (atval.equals("NTIM")) {
                            ntim++;
                        }
                        if (atval.equals("NDAT")) {
                            ndat++;
                        }
                        if (atval.equals("NDAY")) {
                            nday++;
                        }
                        if (atval.equals("NMON")) {
                            nmon++;
                        }
                        if (atval.equals("NNUM")) {
                            nnum++;
                        }
                        if (atval.equals("NTEL")) {
                            ntel++;
                        }
                        if (atval.equals("NDIG")) {
                            ndig++;
                        }
                        if (atval.equals("NSCR")) {
                            nscr++;
                        }
                        if (atval.equals("NRGN")) {
                            nrgn++;
                        }
                        if (atval.equals("NFRC")) {
                            nfrc++;
                        }
                        if (atval.equals("NADD")) {
                            nadd++;
                        }
                        if (atval.equals("NCOM")) {
                            ncom++;
                        }
                    }
            }
        }

        System.out.println("    +nadd: " + nadd);
        System.out.println("    +ncom: " + ncom);
        System.out.println("    +ndat: " + ndat);

        System.out.println("    +nday: " + nday);
        System.out.println("    +ndig: " + ndig);
        System.out.println("    +nfrc: " + nfrc);
        System.out.println("    +nmon: " + nmon);
        System.out.println("    +nnum: " + nnum);
        System.out.println("    +nargn: " + nrgn);
        System.out.println("    +nscr: " + nscr);
        System.out.println("    +ntel: " + ntel);
        System.out.println("    +ntim: " + ntim);


        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("Thoi gian gan the num la: " + time);
    }

    public void countCroup(String inf) throws XMLStreamException, IOException {
        long startTime = System.currentTimeMillis();
        int num = 0;
        int alpha = 0;
        int other = 0;

        FileInputStream fis = new FileInputStream(inf);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("W")) {
                        String atval = xmlStreamReader.getAttributeValue(0);
                        if (atval.equals("NUMBER")) {
                            num++;
                        }
                        if (atval.equals("ALPHA")) {
                            alpha++;
                        }
                        if (atval.equals("OTHER")) {
                            other++;
                        }
                    }
            }
        }

        System.out.println("    +num: " + num);
        System.out.println("    +alpha: " + alpha);
        System.out.println("    +other: " + other);


        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("Thoi gian gan the num la: " + time);

    }

    public void countPunc(String inf) throws XMLStreamException, IOException {
        long startTime = System.currentTimeMillis();
        int ngang = 0;
        int cheo = 0;

        System.out.println("package Count; class Count");

        FileInputStream fis = new FileInputStream(inf);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("W")) {
                        String nsw = xmlStreamReader.getElementText();
                        Pattern n = Pattern.compile("\\p{Pd}");
                        Matcher m = n.matcher(nsw);

                        if (m.find()) {
                            ngang++;
                        }

                        if (nsw.equals("/")) {
                            cheo++;
                        }
                    }
            }
        }

        System.out.println("    +ngang: " + ngang);
        System.out.println("    +cheo: " + cheo);

        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("Thoi gian gan the num la: " + time);

    }
}
